
## This might not be the best implementation. Especially the installation part.
## Should be improved later.

# Generate the file containing commandline help (--help).
# Note that this tries to run OMSimulator from the build tree. right now this works fine because
# everything the OMSimulator depends on (in this project) is linked statically to it. However, for example,
# if it is changed to link to a shared OMSimulatorLib, then it will not run from the build tree
# because it will nto find the shared lib. In that case we need to first install the project and then
# use the installed OMSimulator here.
add_custom_target(doc-generate-cmd-help-file
                  COMMAND $<TARGET_FILE:OMSimulator> --help > ${CMAKE_CURRENT_SOURCE_DIR}/source/omsimulator-help.inc

                  BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/source/omsimulator-help.inc
                  COMMENT "Generating omsimulator-help.inc.")



####################
## PDF

# Generate latex files
add_custom_target(doc-sphinx-latex
                  COMMAND ${SPHINX_EXECUTABLE}
                                -b latex
                                -d ${CMAKE_CURRENT_BINARY_DIR}/doctrees-latex
                                ${CMAKE_CURRENT_SOURCE_DIR}/source ${CMAKE_CURRENT_BINARY_DIR}/latex
                  BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/doctrees-latex
                  BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/latex
                  COMMENT "Generating latex files with Sphinx.")

add_dependencies(doc-sphinx-latex doc-generate-cmd-help-file)

# Generate pdf from the latex files. This just goes to the directory generated by
# sphinx and calls 'make all-pdf'
add_custom_target(doc-sphinx-latex-pdf
                  COMMAND make all-pdf
                  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/latex
                  COMMENT "Compiling latex files to PDF.")

add_dependencies(doc-sphinx-latex-pdf doc-sphinx-latex)

# Install target for pdf documentation
add_custom_target(install-sphinx-pdf-docs
                  COMMAND ${CMAKE_COMMAND} -E copy
                        ${CMAKE_CURRENT_BINARY_DIR}/latex/OMSimulator.pdf
                        ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DOCDIR}/UsersGuide/pdf/OMSimulator.pdf)

add_dependencies(install-sphinx-pdf-docs doc-sphinx-latex-pdf)


####################
## HTML

# Generate html files
add_custom_target(doc-sphinx-html
                  COMMAND ${SPHINX_EXECUTABLE}
                                -b html
                                -d ${CMAKE_CURRENT_BINARY_DIR}/doctrees-html
                                ${CMAKE_CURRENT_SOURCE_DIR}/source ${CMAKE_CURRENT_BINARY_DIR}/html
                  BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/doctrees-html
                  BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/html
                  COMMENT "Generating html documentation with Sphinx.")

add_dependencies(doc-sphinx-html doc-generate-cmd-help-file)

# Install target for html documentation
add_custom_target(install-sphinx-html-docs
                  COMMAND ${CMAKE_COMMAND} -E copy_directory
                        ${CMAKE_CURRENT_BINARY_DIR}/html/
                        ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DOCDIR}/UsersGuide/html/)

add_dependencies(install-sphinx-html-docs doc-sphinx-html)


###################################################
# Install target for all sphinx documentation
add_custom_target(install-sphinx-docs)
add_dependencies(install-sphinx-docs install-sphinx-html-docs install-sphinx-pdf-docs)
